package snake;

import boogie.game.sound.SoundManager;
import com.jtattoo.plaf.hifi.HiFiLookAndFeel;
import java.awt.event.*;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Boogie
 */
public class MainMenu extends javax.swing.JFrame {

    /**
     * Creates new form MainMenu
     */
    private MainGame g;

    public MainMenu() {
        
        initComponents();
        this.setResizable(false);
        ChangeListener l = new ChangeListener() {
            
            @Override
            public void stateChanged(ChangeEvent e) {
                speedSliderVal.setText("Current Speed: " + speedSlider.getValue());
            }
        };
        this.speedSlider.addChangeListener(l);
        this.setTitle("Snake");
        g = new MainGame();
        g.addWindowListener(new WindowAdapter() {
            
            @Override
            public void windowClosing(WindowEvent e) {                
                MainMenu.this.setVisible(true);
                g.stopGame();
            }
        });
        
        this.startBtn.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                MainMenu.this.setVisible(false);
                g.startGame(MainMenu.this.speedSlider.getValue());                
            }
        });
        this.muteBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                // Set "ignore" whenever box is checked or unchecked.
                SoundManager.getInstance().setMute((e.getStateChange() == ItemEvent.SELECTED));
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        speedSlider = new javax.swing.JSlider();
        speedSliderVal = new javax.swing.JLabel();
        startBtn = new javax.swing.JButton();
        muteBox = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        speedSlider.setMajorTickSpacing(3);
        speedSlider.setMaximum(10);
        speedSlider.setMinimum(1);
        speedSlider.setMinorTickSpacing(1);
        speedSlider.setValue(5);

        speedSliderVal.setText("Current Speed: 5");

        startBtn.setText("Start Game");

        muteBox.setText("Mute");
        muteBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                muteBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(startBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(speedSlider, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(speedSliderVal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(muteBox))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(startBtn)
                .addGap(18, 18, 18)
                .addComponent(speedSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(speedSliderVal, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(muteBox)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void muteBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_muteBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_muteBoxActionPerformed

    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(new HiFiLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            
            @Override
            public void run() {
                new MainMenu().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox muteBox;
    private javax.swing.JSlider speedSlider;
    private javax.swing.JLabel speedSliderVal;
    private javax.swing.JButton startBtn;
    // End of variables declaration//GEN-END:variables
}
